<template>
  <div class="carModels">
    <div class="container">
      <!-- 顶部搜索框 -->
      <div class="top">
        <el-row type="flex" align="middle">
          <span>车型编号：</span>
          <el-input
            v-model="id"
            placeholder="请输入车辆编号"
            clearable
            style="width: 200px"
          />
          <span>应载重量：</span>
          <el-select v-model="allowableLoad" placeholder="请选择应载重量">
            <el-option
              v-for="item in allowableLoadList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
          <span>应载体积：</span>
          <el-select v-model="allowableVolume" placeholder="请选择应载体积">
            <el-option
              v-for="item in allowableVolumeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-row>
        <el-row type="flex" align="middle">
          <span>车辆类型：</span>
          <el-input
            v-model="name"
            placeholder="请输入车辆类型"
            clearable
            style="margin-right: 20px; width: 200px"
          />
          <el-button
            type="primary"
            size="mini"
            @click="btnSearch"
          >搜索</el-button>
          <el-button size="mini" @click="reset">重置</el-button>
        </el-row>
      </div>
    </div>
    <div class="container">
      <div class="table">
        <el-button type="primary" size="small" @click="add">新增车型</el-button>
        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" :index="indexMethod" />
          <el-table-column prop="id" label="车型编号" />
          <el-table-column prop="name" label="车辆类型" />
          <el-table-column prop="num" label="车型数量" />
          <el-table-column prop="allowableLoad" label="应载重量(吨)" />
          <el-table-column prop="allowableVolume" label="应载体积(立方米)" />
          <el-table-column prop="measureLong" label="长(米)" />
          <el-table-column prop="measureWidth" label="宽(米)" />
          <el-table-column prop="measureHigh" label="高(米)" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                type="text"
                style="color: blue"
                @click="update(row.id)"
              >编辑</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
          :current-page="tableParams.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="tableParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div class="container">
      <!-- 新增、编辑运费弹窗 -->
      <freight-dialog
        ref="freightDialog"
        :title="title"
        :show-dialog.sync="showDialog"
        :detail-id="detailId"
        @update="getCarModelsList()"
      />
    </div>
  </div>
</template>

<script>
import { calItemNum } from '@/utils/page'
import { getCarModelsList, delCarModelsList } from '@/api/CarModels'
import freightDialog from './freight-dialog.vue'
// 车型载重对应关系:
const allowableLoadList = [
  {
    value: '全部',
    name: null
  },
  {
    value: 'RANGE_LEVEL_1',
    name: '<1.8（吨）'
  },
  {
    value: 'RANGE_LEVEL_2',
    name: '1.8-6（吨）'
  },
  {
    value: 'RANGE_LEVEL_3',
    name: '6-14（吨）'
  },
  {
    value: 'RANGE_LEVEL_4',
    name: '14-30（吨）'
  },
  {
    value: 'RANGE_LEVEL_5',
    name: '30-50（吨）'
  },
  {
    value: 'RANGE_LEVEL_6',
    name: '50-100（吨）'
  },
  {
    value: 'RANGE_LEVEL_7',
    name: '100>（吨）'
  }
]
// 车型体积对应关系:
const allowableVolumeList = [
  {
    value: '全部',
    name: null
  },
  {
    value: 'RANGE_LEVEL_1',
    name: '<3（m³）'
  },
  {
    value: 'RANGE_LEVEL_2',
    name: '3-5（m³）'
  },
  {
    value: 'RANGE_LEVEL_3',
    name: '5-10（m³）'
  },
  {
    value: 'RANGE_LEVEL_4',
    name: '10-15（m³）'
  },
  {
    value: 'RANGE_LEVEL_5',
    name: '15-30（m³）'
  },
  {
    value: 'RANGE_LEVEL_6',
    name: '30-50（m³）'
  },
  {
    value: 'RANGE_LEVEL_7',
    name: '50-80（m³）'
  },
  {
    value: 'RANGE_LEVEL_8',
    name: '80-150（m³）'
  },
  {
    value: 'RANGE_LEVEL_9',
    name: '150>（m³）'
  }
]
export default {
  components: {
    freightDialog
  },
  data() {
    return {
      total: 1, // 总数据
      // 表格数据请求参数
      tableParams: {
        page: 1,
        pageSize: 10
      },
      name: '', // 车辆类型
      id: null, // 车辆编号
      allowableLoadList, // 车型载重对应关系:
      allowableLoad: '', // 车型载重
      allowableVolumeList, // 车型体积对应关系:
      allowableVolume: '', // 车型体积
      tableData: [], // 表格数据数组
      // 弹窗
      title: '新增车型',
      showDialog: false, // 弹窗状态
      detailId: '' // 车型详情id
    }
  },
  created() {
    this.getCarModelsList()
  },
  methods: {
    // 获取表格数据
    async getCarModelsList() {
      const result = await getCarModelsList(this.tableParams)
      // const result = await getCarModelsList2()
      // console.log(result)
      // console.log(result.data.counts)
      this.total = +result.data.counts
      this.tableData = result.data.items
    },
    // 分页器
    handleSizeChange(val) {
      // console.log(val)
      this.tableParams.pageSize = val + ''
      this.getCarModelsList()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.tableParams.page = val + ''
      this.getCarModelsList()
    },
    // 搜索
    async btnSearch() {
      this.tableParams = {
        ...this.tableParams,
        allowableLoad: this.allowableLoad,
        allowableVolume: this.allowableVolume,
        id: this.id,
        name: this.name
      }
      // console.log(this.tableParams)
      await this.getCarModelsList(this.tableParams)
      // this.$message.success('搜索成功')
    },
    // 重置搜索框
    reset() {
      this.allowableLoad = ''
      this.allowableVolume = ''
      this.id = null
      this.name = ''
      this.btnSearch()
    },
    // 计算序号
    indexMethod(index) {
      return calItemNum(
        index,
        this.tableParams.page,
        this.tableParams.pageSize
      )
    },
    // 新增车型
    add() {
      // console.log('新增')
      this.title = '新增车型'
      this.showDialog = true
    },
    // 删除
    del(id) {
      // console.log('删除', id)
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delCarModelsList(+id)
          // 刷新列表
          this.getCarModelsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch((e) => {
          console.dir(e)
          this.$message.error('绑定车辆的车型无法删除')
        })
    },
    // 编辑
    update(id) {
      // console.log('编辑', id)
      this.detailId = id
      this.$nextTick(async() => {
        await this.$refs.freightDialog.getCarModelsDetail()
        this.title = '编辑车型'
        this.showDialog = true
      })
    }
  }
}
</script>

<style scoped lang="scss">
.carModels {
  background-color: #eee;
  height: 100vh;
  .container {
    margin: 20px auto;
    //版心尺寸
    //width: 1700px;

    .top {
      margin: 10px;
      background-color: #fff;
      padding: 10px;
      span {
        margin: 10px;
      }
      .el-input {
        margin: 10px;
      }
    }
    .table {
      background-color: #fff;
      padding: 10px;
      margin: 10px;
    }
  }
}
</style>
